package cn.wolfcode.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by Administrator on 2020/5/6.
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    //查询
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")QueryObject qo){
        model.addAttribute("pageInfo",roleService.query(qo));
        return "role/list";
    }
    //编辑,跳转到新增或者修改的jsp
    @RequestMapping("/input")
    public String input(Model model,Long id){
        //查询权限
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        if(id != null){
            model.addAttribute("role",roleService.get(id));
        }
        return "role/input";
    }
    //保存修改
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if(role.getId() != null){
            roleService.update(role,ids);
        }else {
            roleService.save(role,ids);
        }
        return "redirect:/role/list.do";
    }
    //删除
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id != null){
            roleService.delete(id);
        }
        return "redirect:/role/list.do";
    }

}
